#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <cmath>

using namespace std;


const int gcNumCam = 12;
const int gcNumFrame = 100;


void Segmentation()
{
	char srcImgName[200];
	char resImgName[200];
	char bnrImgName[200];

	char dir[200] = "I:/SL/FireData/131225/exp2/image";

	IplImage *srcImg, *resImg, *bnrImg;
	for (int camIdx = 0; camIdx < gcNumCam; ++camIdx)
	{
		printf("Processing camera %d\n", camIdx);

		for (int frameIdx = 0; frameIdx < gcNumFrame; ++frameIdx)
		{


			sprintf(srcImgName, "%s/frame_aligned/camera%d/%d.png", dir, camIdx, frameIdx);
			sprintf(resImgName, "%s/background_removed/camera%d/rec_src_%d.jpg", dir, camIdx, frameIdx);
			sprintf(bnrImgName, "%s/binary_img/camera%d/bnr_%d.jpg", dir, camIdx, frameIdx);

			srcImg = cvLoadImage(srcImgName, CV_LOAD_IMAGE_COLOR);
			resImg = cvCreateImage(cvSize(srcImg->width, srcImg->height), 8, 3);
			bnrImg = cvCreateImage(cvSize(srcImg->width, srcImg->height), 8, 1);


			for (int i = 0; i < srcImg->height; ++i)
			{
				for (int j = 0; j < srcImg->width; ++j)
				{
					if ((unsigned char)(srcImg->imageData[i * srcImg->widthStep + j * srcImg->nChannels + 2]) > 30)
					{
						resImg->imageData[i * resImg->widthStep + j * resImg->nChannels + 2]
						= srcImg->imageData[i * srcImg->widthStep + j * srcImg->nChannels + 2];
						resImg->imageData[i * resImg->widthStep + j * resImg->nChannels + 1]
						= srcImg->imageData[i * srcImg->widthStep + j * srcImg->nChannels + 1];
						resImg->imageData[i * resImg->widthStep + j * resImg->nChannels + 0]
						= srcImg->imageData[i * srcImg->widthStep + j * srcImg->nChannels + 0];

						bnrImg->imageData[i * bnrImg->widthStep + j * bnrImg->nChannels] = 255;
					}
					else
					{
						resImg->imageData[i * resImg->widthStep + j * resImg->nChannels + 2] = 0;
						resImg->imageData[i * resImg->widthStep + j * resImg->nChannels + 1] = 0;
						resImg->imageData[i * resImg->widthStep + j * resImg->nChannels + 0] = 0;

						bnrImg->imageData[i * bnrImg->widthStep + j * bnrImg->nChannels] = 0;
					}
				}
			}

			cvSaveImage(resImgName, resImg);
			cvSaveImage(bnrImgName, bnrImg);

			cvReleaseImage(&srcImg);
			cvReleaseImage(&bnrImg);
			cvReleaseImage(&resImg);


		}
	}

}


void AlignFrame()
{
	char srcImgName[200];
	char resImgName[200];
	char bnrImgName[200];

	//char dir[200] = "J:/SL/FireData/Group V/FireData/fire";
	//char dir[200] = "E:/fire/Video";
	char dir[200] = "I:/SL/FireData/131225/exp2/image";


	IplImage *srcImg;
	for (int camIdx = 0; camIdx < gcNumCam; ++camIdx)
	{
		printf("Processing camera %d\n", camIdx);

		for (int frameIdx = 0; frameIdx < gcNumFrame; ++frameIdx)
		{


			if (0 == camIdx)
			{
				//sprintf(srcImgName, "J:/SL/FireData/Group V/FireData/fire/%d/%d.png", camIdx, frameIdx + 5);
				sprintf(srcImgName, "%s/camera%d/%d.png", dir, camIdx, frameIdx + 9440);
			}
			else if (1 == camIdx)
			{
				//sprintf(srcImgName, "J:/SL/FireData/Group V/FireData/fire/%d/%d.png", camIdx, frameIdx + 5);
				sprintf(srcImgName, "%s/camera%d/%d.png", dir, camIdx, frameIdx + 9379);
			}
			else if (2 == camIdx)
			{
				//sprintf(srcImgName, "J:/SL/FireData/Group V/FireData/fire/%d/%d.png", camIdx, frameIdx + 5);
				sprintf(srcImgName, "%s/camera%d/%d.png", dir, camIdx, frameIdx + 9222);
			}
			else if (3 == camIdx)
			{
				//sprintf(srcImgName, "J:/SL/FireData/Group V/FireData/fire/%d/%d.png", camIdx, frameIdx + 5);
				sprintf(srcImgName, "%s/camera%d/%d.png", dir, camIdx, frameIdx + 9166);
			}
			else if (4 == camIdx)
			{
				//sprintf(srcImgName, "J:/SL/FireData/Group V/FireData/fire/%d/%d.png", camIdx, frameIdx + 5);
				sprintf(srcImgName, "%s/camera%d/%d.png", dir, camIdx, frameIdx + 8994);
			}
			else if (5 == camIdx)
			{
				//sprintf(srcImgName, "J:/SL/FireData/Group V/FireData/fire/%d/%d.png", camIdx, frameIdx + 5);
				sprintf(srcImgName, "%s/camera%d/%d.png", dir, camIdx, frameIdx + 8908);
			}
			else if (6 == camIdx)
			{
				//sprintf(srcImgName, "J:/SL/FireData/Group V/FireData/fire/%d/%d.png", camIdx, frameIdx + 5);
				sprintf(srcImgName, "%s/camera%d/%d.png", dir, camIdx, frameIdx + 8763);
			}
			else if (7 == camIdx)
			{
				//sprintf(srcImgName, "J:/SL/FireData/Group V/FireData/fire/%d/%d.png", camIdx, frameIdx + 5);
				sprintf(srcImgName, "%s/camera%d/%d.png", dir, camIdx, frameIdx + 8680);
			}
			else if (8 == camIdx)
			{
				//sprintf(srcImgName, "J:/SL/FireData/Group V/FireData/fire/%d/%d.png", camIdx, frameIdx + 5);
				sprintf(srcImgName, "%s/camera%d/%d.png", dir, camIdx, frameIdx + 8466);
			}
			else if (9 == camIdx)
			{
				//sprintf(srcImgName, "J:/SL/FireData/Group V/FireData/fire/%d/%d.png", camIdx, frameIdx + 5);
				sprintf(srcImgName, "%s/camera%d/%d.png", dir, camIdx, frameIdx + 8421);
			}
			else if (10 == camIdx)
			{
				//sprintf(srcImgName, "J:/SL/FireData/Group V/FireData/fire/%d/%d.png", camIdx, frameIdx + 5);
				sprintf(srcImgName, "%s/camera%d/%d.png", dir, camIdx, frameIdx + 8278);
			}
			else if (11 == camIdx)
			{
				//sprintf(srcImgName, "J:/SL/FireData/Group V/FireData/fire/%d/%d.png", camIdx, frameIdx + 5);
				sprintf(srcImgName, "%s/camera%d/%d.png", dir, camIdx, frameIdx + 8236);
			}


			//sprintf(resImgName, "J:\\SL\\FireData\\Group V\\FireData\\fire\\frame_aligned\\%d\\%d.png", camIdx, frameIdx);
			sprintf(resImgName, "%s/frame_aligned/camera%d/%d.png", dir, camIdx, frameIdx);
	
			srcImg = cvLoadImage(srcImgName, CV_LOAD_IMAGE_COLOR);


			cvSaveImage(resImgName, srcImg);


			cvReleaseImage(&srcImg);

		}
	}
}

void substractBackground()
{
	for (int i = 0; i < 16; ++i)
	{
		if (8 == i)
		{
			continue;
		}

		char srcName[200];
		char bgName[200];
		char outName[200];
		sprintf(srcName, "../data/paper2012/input_image/%d.png", i);
		sprintf(bgName, "D:/cloud/lab/paper/SIGGRAPH2012/data/StochasticTomography-vortex-sample/cam%02d/background.png", i);
		sprintf(outName, "../data/paper2012/input_image/view_%d.png", i);

		IplImage* srcImage = cvLoadImage(srcName);
		IplImage* bgImage = cvLoadImage(bgName);
		IplImage* outImage = cvCreateImage(cvGetSize(srcImage), IPL_DEPTH_8U, 1);
		for (int y = 0; y < outImage->height; ++y)
		{
			for (int x = 0; x < outImage->width; ++x)
			{
				outImage->imageData[outImage->widthStep * y + x]
					= max(0,
					int(srcImage->imageData[srcImage->widthStep  * y + x * srcImage->nChannels]
					- bgImage->imageData[bgImage->widthStep * y + x * bgImage->nChannels]));
			}
		}
		cvSaveImage(outName, outImage);
		cvReleaseImage(&srcImage);
		cvReleaseImage(&bgImage);
		cvReleaseImage(&outImage);
	}
}

void getInputImage()
{
	char name[200];
	char outName[200];
	for (int i = 0; i < 15; ++i)
	{
		if (i < 8)
		{
			sprintf(name, "../data/paper2012/input_image/view_%d.jpg", i);
		}
		else
		{
			sprintf(name, "../data/paper2012/input_image/view_%d.jpg", i + 1);
		}
		IplImage* image = cvLoadImage(name);
		IplImage* outImage = cvCreateImage(cvSize(image->width, image->height), IPL_DEPTH_8U, 3);
		memset(outImage->imageData, 0, image->width * image->height * 3);
		for (int y = 0; y < image->height; ++y)
		{
			for (int x = 0; x < image->width; ++x)
			{
				if (uchar(image->imageData[y * image->widthStep + x * image->nChannels]) > 10)
				{
					outImage->imageData[y * outImage->widthStep + x * outImage->nChannels]
						= image->imageData[y * image->widthStep + x * image->nChannels];
					outImage->imageData[y * outImage->widthStep + x * outImage->nChannels + 1]
						= image->imageData[y * image->widthStep + x * image->nChannels + 1];
					outImage->imageData[y * outImage->widthStep + x * outImage->nChannels + 2]
						= image->imageData[y * image->widthStep + x * image->nChannels + 2];
				}
			}
		}
		sprintf(outName, "../data/paper2012/input_image/bg_sub/view_%d.jpg", i);
		cvSaveImage(outName, outImage);
		cvReleaseImage(&image);
		cvReleaseImage(&outImage);

	}
}

void getTemplate()
{
	char name[200];
	char outName[200];
	for (int i = 0; i < 15; ++i)
	{
		if (i < 8)
		{
			sprintf(name, "../data/paper2012/input_image/view_%d.jpg", i);
		}
		else
		{
			sprintf(name, "../data/paper2012/input_image/view_%d.jpg", i + 1);
		}
		IplImage* image = cvLoadImage(name);
		IplImage* tem = cvCreateImage(cvSize(image->width, image->height), IPL_DEPTH_8U, 1);
		memset(tem->imageData, 0, image->width * image->height);
		for (int y = 0; y < image->height; ++y)
		{
			for (int x = 0; x < image->width; ++x)
			{
				if (uchar(image->imageData[y * image->widthStep + x * image->nChannels]) > 10)
				{
					tem->imageData[y * tem->widthStep + x * tem->nChannels] = 127;
				}
			}
		}
		sprintf(outName, "../data/paper2012/input_image/tem_%d.jpg", i);
		cvSaveImage(outName, tem);
		cvReleaseImage(&image);
		cvReleaseImage(&tem);

	}
}